# -------------------------------------------------------------------------------------------------
# Copyright 2024 Kearn Chen, kearn.chen@aliyun.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------------------------------

	.section .init, "ax", @progbits
	.globl  _start
	.align  2
_start:
	.option norvc;
    j       handler_reset
    .word   handler_irq0
    .word   handler_irq1
    .word   handler_irq2
    .word   handler_irq3
    .word   handler_irq4
    .word   handler_irq5
    .word   handler_irq6
    .word   handler_irq7
    .word   handler_irq8
    .word   handler_irq9
    .word   handler_irq10
    .word   handler_irq11
    .word   handler_irq12
    .word   handler_irq13
    .word   handler_irq14
    .word   handler_irq15

	.section .text.handler_isr, "ax", @progbits
	.align    2
handler_isr:
    addi sp, sp, -56
    sw x1 , 0(sp)
    sw x4 , 4(sp)
    sw x5 , 8(sp)
    sw x6 , 12(sp)
    sw x7 , 16(sp)
    sw x8 , 20(sp)
    sw x9 , 24(sp)
    sw x10, 28(sp)
    sw x11, 32(sp)
    sw x12, 36(sp)
    sw x13, 40(sp)
    sw x14, 44(sp)
    sw x15, 48(sp)
1:
    csrr a0, mcause
    beq x0, a0, 2f
    andi a0, a0, 15
    addi a1, x0, 1
    sll a2, a1, a0
    csrrc a2, mip, a2
    slli a0, a0, 2
    lw a1, 4(a0)
    jalr ra, a1, 0
    jal x0, 1b
2:
    lw x15, 48(sp)
    lw x14, 44(sp)
    lw x13, 40(sp)
    lw x12, 36(sp)
    lw x11, 32(sp)
    lw x10, 28(sp)
    lw x9 , 24(sp)
    lw x8 , 20(sp)
    lw x7 , 16(sp)
    lw x6 , 12(sp)
    lw x5 , 8(sp)
    lw x4 , 4(sp)
    lw x1 , 0(sp)
    addi sp, sp, 56
    mret

	.section .text.handler_default, "ax", @progbits
	.align    2
	.weak   handler_irq0
	.weak   handler_irq1
	.weak   handler_irq2
	.weak   handler_irq3
	.weak   handler_irq4
	.weak   handler_irq5
	.weak   handler_irq6
	.weak   handler_irq7
	.weak   handler_irq8
	.weak   handler_irq9
	.weak   handler_irq10
	.weak   handler_irq11
	.weak   handler_irq12
	.weak   handler_irq13
	.weak   handler_irq14
	.weak   handler_irq15
handler_irq0:
handler_irq1:
handler_irq2:
handler_irq3:
handler_irq4:
handler_irq5:
handler_irq6:
handler_irq7:
handler_irq8:
handler_irq9:
handler_irq10:
handler_irq11:
handler_irq12:
handler_irq13:
handler_irq14:
handler_irq15:
loop_isr:
	j loop_isr

	.section  .text.handler_reset, "ax", @progbits
	.align    2
handler_reset:
.option push
.option norelax
	la gp, __global_pointer$
.option pop
1:
	la sp, _eusrstack
2:
	la a0, _data_lma
	la a1, _data_vma
	la a2, _edata
	bgeu a1, a2, 2f
1:
	lw a3, 0(a0)
	sw a3, 0(a1)
	addi a0, a0, 4
	addi a1, a1, 4
	bgeu a1, a2, 2f
	lw a3, 0(a0)
	sw a3, 0(a1)
	addi a0, a0, 4
	addi a1, a1, 4
	bltu a1, a2, 1b
2:
    la a0, _sbss
    la a1, _ebss
    bgeu a0, a1, 2f
1:
    sw x0, 0(a0)
    addi a0, a0, 4
    bgeu a0, a1, 2f
    sw x0, 0(a0)
    addi a0, a0, 4
    bgeu a0, a1, 2f
    sw x0, 0(a0)
    addi a0, a0, 4
    bltu a0, a1, 1b
2:
    la a3, handler_isr
    csrw mtvec, a3

    j main
